package com.wcity.manage.service.impl;

import com.wcity.manage.common.utils.DigestUitl;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.RoleMapper;
import com.wcity.manage.dao.mapper.UserMapper;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户服务层
 *
 * @author dy
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public User getUser(String username ) {
        User user = userMapper.findByUserName( username );
        if ( user != null ) user.setRole( roleMapper.findById( user.getRoleId() ) );
        return user;
    }

    @Override
    public List<User> getUserListPage( PageModel<User> page ) {
        return userMapper.findListPage( page );
    }

    @Override
    public User getUser( int id ) {
        User user = userMapper.find( id );
        if ( user != null ) user.setRole( roleMapper.findById( user.getRoleId() ) );
        return user;
    }

    @Override
    public String addUser( User user ) {

        User local = userMapper.findByUserName(user.getUserName());
        if ( local != null ) return "用户已存在";

        user.setPwd( DigestUitl.pwd( user.getPwd() ) );
        try {
            if ( userMapper.insert( user ) <= 0 ) return "添加用户失败";
        } catch ( DataAccessException e ) {
            log.error( "addUser error, {}", e.getMessage(), e );
            return "添加用户失败";
        }
        return "";
    }

    @Override
    public void updateLastLoginTime( int id ) {
        userMapper.updateLastLoginTime( id, new Date()  );
    }

    @Override
    public String updateUser( User user, int id ) {
        if ( !StringUtil.isBlank( user.getPwd() ) ) {
            user.setPwd( DigestUitl.pwd( user.getPwd() ) );
        }
        user.setId( id );
        try {
            if ( userMapper.update( user ) <= 0 ) return "没有更新";
        } catch ( DataAccessException e ) {
            log.error( "updateUser error, {}", e.getMessage(), e );
            return "更新用户失败";
        }
        return "";
    }

    @Override
    public String editUserInfo( User localUser, String oriPwd, User user ) {

        if ( StringUtil.isBlank( oriPwd ) || !DigestUitl.pwd( oriPwd ).equals( localUser.getPwd() ) ) {
            return "原密码不正确";
        }
        user.setPwd( DigestUitl.pwd( user.getPwd() ) );
        user.setId( localUser.getId() );
        user.setIsAvailable(true);

        if ( DigestUitl.pwd( oriPwd ).equals( user.getPwd() ) ) {
            return "新密码与原密码相同";
        }

        try {
            if ( userMapper.update( user ) <= 0 ) return "没有更新";
        } catch ( DataAccessException e ) {
            log.error( "updateUser error, {}", e.getMessage(), e );
            return "更新用户失败";
        }
        return "";
    }

    @Resource private UserMapper userMapper;
    @Resource private RoleMapper roleMapper;

    private final static Logger log = LoggerFactory.getLogger( UserServiceImpl.class );

}
